package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Office;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

public class OfficesBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+UsersBO.class.getSimpleName();
	
	public Office insertOffice(Office office, EntityManager entityManager) throws SystemException {
		if (this.searchOfficeByName(office, entityManager) == null) {
			if (office.getId() == null) {
				office.setId(0);
			}
			office.setName(office.getName().toLowerCase());
			entityManager.persist(office);
			return this.searchOfficeByName(office, entityManager);
		}
		return null;
	}

	
	public List<Office> listOffices(EntityManager entityManager) throws SystemException {
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_OFFICES);
		
		List<Office> offices = query.getResultList();
		
		if (offices.size() > 0) {
			Office office = new Office();
			office.setId(1);
			
			offices.remove(office);
		}
		
		return offices;
	}

	
	public boolean removeOffice(Office office, EntityManager entityManager) throws SystemException {
		if (office == null) {
			throw new SystemException(new Exception("null office"),CLASS_NAME,"removeOffice");
		}
		if (office.getId().equals(1)) {
			return false;
		}
		if (!(office.getId() == null)) {
			office = this.searchOfficeById(office, entityManager);
			entityManager.remove(office);
			return true;
		} else {
			if (!(office.getName() == null)) {
				office = this.searchOfficeByName(office, entityManager);
				entityManager.remove(office);
				return true;
			} else {
				throw new SystemException(new Exception("null office.id or office.name"), CLASS_NAME, "removeOffice");
			}
		}
	}

	
	public Office searchOfficeById(Office office, EntityManager entityManager) throws SystemException {
		if (office == null) {
			throw new SystemException(new Exception("null office"),CLASS_NAME,"searchOfficeById");
		}
		if (office.getId() == null) {
			throw new SystemException(new Exception("null office.id"), CLASS_NAME, "searchOfficeById");
		}
		if (office.getId().equals(1)) {
			return null;
		}
		return entityManager.find(Office.class, office.getId());
	}

	
	public Office searchOfficeByName(Office office, EntityManager entityManager) throws SystemException {
		if (office == null) {
			throw new SystemException(new Exception("null office"),CLASS_NAME,"searchOfficeById");
		}
		if (office.getName() == null) {
			throw new SystemException(new Exception("null office.name"), CLASS_NAME, "searchOfficeById");
		}
		
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_OFFICE_BY_NAME);
		query.setParameter(1, office.getName().toLowerCase());
		
		List<Office> offices = query.getResultList();
		
		if (offices.size() > 0) {
			office = offices.get(0);
			if (office.getId().equals(1)) {
				return null;
			}
			return office;
		}
		return null;
	}

	
	public Office updateOffice(Office office, EntityManager entityManager) throws SystemException {
		Office tempOffice = this.searchOfficeById(office, entityManager);
		if (!(tempOffice == null)) {
//			if (!(office.getName() == null)) {
//				tempOffice.setName(office.getName());
//			}
			if (!(office.getAddress() == null)) {
				tempOffice.setAddress(office.getAddress());
			}
			if (!(office.getCity() == null)) {
				tempOffice.setCity(office.getCity());
			}
			tempOffice.setImage(office.getImage());
			tempOffice.setImagePath(office.getImagePath());
						
			return tempOffice;
		} else {
			tempOffice = this.searchOfficeByName(office, entityManager);
			if (!(tempOffice == null)) {
				if (!(office.getAddress() == null)) {
					tempOffice.setAddress(office.getAddress());
				}
				if (!(office.getCity() == null)) {
					tempOffice.setCity(office.getCity());
				}
				if (!(office.getName() == null)) {
					tempOffice.setName(office.getName());
				}
				tempOffice.setImage(office.getImage());
				tempOffice.setImagePath(office.getImagePath());
								
				return tempOffice;
			}
		}
		return null;
	}
}
